package com.kingsoft.dc.khaos.module.mpp.sqlbuilder;

import java.util.List;

/**
 * 贴源增量求全量sql构建.
 */
public class GetAllSqlBuilder {

    /**
     * 构建增量求全量sql语句，源表为增量
     * @param srcTable 本期增量表
     * @param targetTable 上期全量表
     * @return
     */
    public static String getAddSelectSql( String srcTable, String targetTable, List<String> keyList) {
        System.out.println("GetAllSqlBuilder getAddSelectSql start...");
        String addTableAlias = "s";
        String allTableAlias = "t";
        String joinKeys = SqlBuilderUtils.joinKeys(addTableAlias, allTableAlias, keyList);
        String where = SqlBuilderUtils.keyIsNull(keyList, addTableAlias);
        String selectSql = String.format("SELECT %s.* FROM %s as %s LEFT JOIN %s as %s ON %s WHERE %s", allTableAlias, targetTable, allTableAlias, srcTable, addTableAlias, joinKeys, where);
        String union = String.format("SELECT %s.* FROM %s as %s", addTableAlias, srcTable, addTableAlias);
        selectSql = selectSql + " UNION ALL " + union;
        System.out.println("GetAllSqlBuilder getAddSelectSql end...");
        return selectSql;
    }

    /**
     * 构建增量求全量sql语句，源表为减量
     * @param srcTable 本期增量表
     * @param targetTable 上期全量表
     * @return
     */
    public static String getDelSelectSql( String srcTable, String targetTable, List<String> keyList) {
        System.out.println("GetAllSqlBuilder getDelSelectSql start...");
        String addTableAlias = "s";
        String allTableAlias = "t";
        String joinKeys = SqlBuilderUtils.joinKeys(addTableAlias, allTableAlias, keyList);
        String where = SqlBuilderUtils.keyIsNull(keyList, addTableAlias);
        String selectSql = String.format("SELECT %s.* FROM %s as %s LEFT JOIN %s as %s ON %s WHERE %s", allTableAlias, targetTable, allTableAlias, srcTable, addTableAlias, joinKeys, where);
        System.out.println("GetAllSqlBuilder getDelSelectSql end...");
        return selectSql;
    }

}
